package com.xxxx.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class SqlSessionUtils {

    public static SqlSession getSqlSession(){
        SqlSession sqlSession = null;
        try{
            //加载资源
            InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");
            //定义sqlSession构造build
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            //构建sqlSession工厂
            SqlSessionFactory build = sqlSessionFactoryBuilder.build(stream);
            //创建sqlSession对象
            sqlSession = build.openSession(true);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return sqlSession;
    }

}
